import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 2.1
import QtMultimedia 5.0

Window {
    id: root

    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    property bool perfMonitorsLogging: false
    property bool perfMonitorsVisible: true

    property bool isRunning: false

    signal videoFramePainted

    function play() { mediaPlayer.play() }
    function pause() { mediaPlayer.pause() }
    function stop() { mediaPlayer.stop() }

    function toggleplay() {
        console.log(isRunning)
        if (isRunning) {
            pause()
            isRunning = false
        } else {
            play()
            isRunning = true
        }
    }

    onAfterRendering: {
        if (performanceLoader.item)
            performanceLoader.item.qmlFramePainted()
    }

    VideoOutput {
        id: video
        width: 128*3
        height: 72*3

        source: mediaPlayer

        MediaPlayer {
            id: mediaPlayer
            autoPlay: true
            volume: 0.1
//            source: "file:///media/disk/my.mp4"
            source: "rtsp://192.168.5.2:8554/test"
            loops: Audio.Infinite
            onVolumeChanged: {
                volumeSlider.value = mediaPlayer.volume;
            }
            onError: {
                console.log(mediaPlayer.errorString)
            }
        }

        PinchArea {
            anchors.fill: parent
            pinch.target: parent
            pinch.minimumRotation: -90
            pinch.maximumRotation: 90
            pinch.minimumScale: 1
            pinch.maximumScale: 3.3
            MouseArea {
                id: dragArea
                anchors.fill: parent
                drag.target: video
                onClicked: toggleplay()
            }
        }
    }


    Row {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 10

        Slider {
            id: volumeSlider
            from: 0
            to: 1
            value: mediaPlayer.volume
            onVisualPositionChanged: {
                mediaPlayer.volume = volumeSlider.visualPosition;
            }
        }

        Switch {
            id: muteSwitch
            onPositionChanged: {
                mediaPlayer.muted = muteSwitch.position == 1;
            }
        }
    }

    Rectangle {
        id: perfHolder
        color: "transparent"
        anchors {
            right: parent.right
            top: parent.top
            margins: 10
        }
        width: 120
        height: 60

        Loader {
            id: performanceLoader
            function init() {
                var enabled = root.perfMonitorsLogging || root.perfMonitorsVisible
                source = enabled ? "qrc:/qml/performancemonitor/PerformanceItem.qml" : ""
            }
            onLoaded: {
                item.parent = perfHolder
                item.anchors.top = perfHolder.top
                item.anchors.bottom = perfHolder.bottom
                item.anchors.left = perfHolder.left
                item.anchors.right = perfHolder.right
                item.logging = root.perfMonitorsLogging
                item.displayed = root.perfMonitorsVisible
                item.init()
            }
        }
    }

    Component.onCompleted: performanceLoader.init();
}
